package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.SolutionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/solution")
public class SolutionController {
    @Autowired
    SolutionService solutionService;

    /**
     * 解决方案分页
     * @param search
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/solution-list")
    @ResponseBody
    public Map<String,Object> SolutionList(String search, Integer pageNum, Integer pageSize, HttpSession session){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1 ||jueNameList.indexOf("销售经理")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }
        solutionService.selectSolution(search,empName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    /**
     * 新增解决方案
     * @param solution
     */
    @RequestMapping("/addSolution")
    @ResponseBody
    public void addSolution(@RequestBody Solution solution){
        solutionService.addSolution(solution);
    }

    /**
     * 查询需要修改的解决方案
     * @param solutionId
     * @return
     */
    @RequestMapping("/upSolution")
    @ResponseBody
    public List<Solution> upSolution(Integer solutionId){
        return solutionService.upSolution(solutionId);
    }


    /**
     * 删除解决方案
     * @param solutionId
     */
    @RequestMapping("/delSolution")
    @ResponseBody
    public void delSolution(Integer solutionId){
        solutionService.delSolution(solutionId);
    }

    /**
     * 查询客户列表
     * @param khsearch
     * @return
     */
    @RequestMapping("/kh-list")
    @ResponseBody
    public Map<String,Object> selectKh(String khsearch,Integer pageNum, Integer pageSize,String empName){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        solutionService.selectKh(khsearch,empName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    /**
     * 对应机会列表
     * @param khName
     * @return
     */
    @RequestMapping("/sales-list")
    @ResponseBody
    public List<Sales> selectSales(String khName){
        return solutionService.selectSales(khName);
    }

    /**
     * 对应需求列表
     * @param SalesId
     * @return
     */
    @RequestMapping("/demand-list")
    @ResponseBody
    public List<Demand> selectDemand(Integer SalesId){
        return solutionService.selectDemand(SalesId);
    }

    /**
     * 对应竞争对手
     * @param SalesId
     * @return
     */
    @RequestMapping("/competitor-list")
    @ResponseBody
    public List<Competitor> selectCompetitor(Integer SalesId){
        return solutionService.selectCompetitor(SalesId);
    }

    /**
     * 销售机会页面已生成解决方案不可删除
     * @param salesId
     * @return
     */
    @RequestMapping("/selectSalesSolution")
    @ResponseBody
    public List<Solution> selectSalesSolution(Integer salesId){
        return solutionService.selectSalesSolution(salesId);
    }

}
